package itbour.onetouchshow.activity.video.make;

import com.alibaba.fastjson.JSON;
import com.lsjr.callback.EncryBeanCallBack;
import com.lsjr.utils.HttpUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import itbour.onetouchshow.App;
import itbour.onetouchshow.base.mvp.BasePresenterImpl;
import itbour.onetouchshow.bean.ModelVideoBean;
import itbour.onetouchshow.bean.inching.noppt.InchingCoreRootNoPPTV10Bean;
import itbour.onetouchshow.bean.inching.noppt.InchingDocStringNoPPTV10Bean;
import itbour.onetouchshow.bean.inching.noppt.InchingRelationsV10Bean;
import itbour.onetouchshow.bean.user.UserMakeTag;
import itbour.onetouchshow.constants.AppConfig;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.utils.L_;
import itbour.onetouchshow.utils.T_;
import itbour.onetouchshow.utils.video.MakeProductContrl;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class MakePresenter extends BasePresenterImpl<MakeContract.View> implements MakeContract.Presenter {

    public MakeProductContrl makeProductContrl;
    //根据docId判断是作品还是模板
    private String docId;
    private MakeActivity makeActivity;

    public MakePresenter(MakeActivity makeActivity, MakeContract.View view) {
        this.makeActivity = makeActivity;
        this.mvpView = view;
    }

    public void setMakeActivity(MakeActivity makeActivity) {
        this.makeActivity = makeActivity;
    }

    public String getDocId() {
        return docId;
    }

    public void setDocId(String docId) {
        this.docId = docId;
    }

    /**
     * 对视频编辑界面的按页数进行排序 不通过objId排序
     *
     * @return
     */
    public Map<Integer, List<InchingCoreRootNoPPTV10Bean>> getWantData(InchingDocStringNoPPTV10Bean videoModuleBean) {


        List<InchingCoreRootNoPPTV10Bean> children = videoModuleBean.getCoreStructure().getRoot().getChildren();
        //先根据objId升序排一遍
        if (children == null || children.size() == 0) {
            Map<Integer, List<InchingCoreRootNoPPTV10Bean>> map = new HashMap<>();
            return map;
        }

        L_.i(children);

        //注册比较器的键值 为升序 键值为pageIndex
        Map<Integer, List<InchingCoreRootNoPPTV10Bean>> map = new TreeMap<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });
        for (int i = 0; i < children.size(); i++) {
            InchingRelationsV10Bean relationsV10Bean = children.get(i).getRelations().get(0);
            Integer key = relationsV10Bean.getPage_index();
            List<InchingCoreRootNoPPTV10Bean> childrenBeans = map.get(key);
            if (childrenBeans == null) {
                childrenBeans = new ArrayList<>();
            }
            childrenBeans.add(children.get(i));
            map.put(key, childrenBeans);
        }


        return map;
    }

    /*
    * 作品预览
    *
    * */
    public void loadDocPreview(String docId) {
        // T_.showToastReal("预览作品ID"+docId);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("device", AppConst.ANDROID_DEVICE);
        stringObjectHashMap.put("userId", App.getUserId());
        stringObjectHashMap.put("docId", docId);
        mvpView.showLoadPreviewPorgress();
        HttpUtils.getInstance().executePost(AppConfig.REQUESTUSERDOCPRIVEWINFO_WEB_V1_1, stringObjectHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                T_.showToastReal(exception);
                L_.e(exception);
                if (mvpView != null) {
                    mvpView.loadPreviewFail(exception);
                }
                MakeProductContrl.instance().oneToucDialogDismiss();
                //OneTouchProgressDialog.getInstance().dismiss();
            }

            @Override
            protected void onSuccess(String response) {
                MakeProductContrl.instance().setInitialize(new MakeProductContrl.MakeProductListener() {

                    @Override
                    public void makeSuccess(String result) {
                        if (mvpView != null) {
                            mvpView.loadPreviewBgSuccess(result);
                        }
                    }

                    @Override
                    public void makeFaile() {
                        T_.showToastReal("生成失败,请稍后再试");
                        makeActivity.MAKE_WORK_STATE = MakeProductContrl.MAKE_FAILE;
                        MakeProductContrl.instance().oneToucDialogDismiss();
                    }

                    @Override
                    public void makeiing(String result) {

                    }

                    @Override
                    public void lookLater() {
                        if (makeActivity != null) {
                            makeActivity.sendRemoveThreeLogic();
                        }
                    }

                    @Override
                    public void isDraft(String result) {

                    }
                }, makeActivity, MakeProductContrl.SignalTag.SHOW_PROGRESS, UserMakeTag.MAKE)
                        .loadNewDocPreviewSuccess(response);

            }
        });
    }


    /**
     * 模板制作修改之后回传DocString
     */
    public void loadMakeVideo(String tmplId, String docString) {
        L_.e("tmplId" + tmplId);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("device", AppConst.ANDROID_DEVICE);
        stringObjectHashMap.put("userId", App.getUserId());
        stringObjectHashMap.put("tmplId", tmplId);
        stringObjectHashMap.put("docString", docString);
        HttpUtils.getInstance().executePost(AppConfig.NEWUSERDOC_V1_0, stringObjectHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                L_.e(exception);
                T_.showCustomToast(exception);
                if (mvpView != null) {
                    mvpView.loadPreviewFail(exception);
                }

            }

            @Override
            protected void onSuccess(String response) {
                L_.e("制作完成" + response);
                //{"productId":366,"docId":683,"opType":2}
                com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(response);
                String docId = jsonObject.getString("docId");
                String productId = jsonObject.getString("productId");
                setDocId(docId);
                // L_.e(docId);
                //  T_.showToastReal("制作完成返回docID"+docId);
                //制作视频后回设productId

                if (mvpView != null) {
                    mvpView.setProductId(Integer.parseInt(productId));
                    mvpView.makeSuccess(response);
                    // loadDocPreview(docId);
                }

            }
        });
    }


    /**
     * 作品修改之后回传DocString
     */
    public void upDataMakeVideo(String docId, String docString) {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("device", AppConst.ANDROID_DEVICE);
        stringObjectHashMap.put("userId", App.getUserId());
        stringObjectHashMap.put("docId", docId);
        stringObjectHashMap.put("docString", docString);
        HttpUtils.getInstance().executePost(AppConfig.UPDATE_USERDOC_V1_0, stringObjectHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                L_.e(exception);
            }

            @Override
            protected void onSuccess(String response) {
                L_.e("制作完成" + response);
                if (mvpView != null) {
                    mvpView.makeSuccess(response);
                    com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(response);
                    String docId = jsonObject.getString("docId");
                    setDocId(docId);
                }
            }
        });
    }


    /*获取作品数据*/
    public void goToMack(ModelVideoBean videoBean, int tag) {
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        L_.e(videoBean.getId() + "");
        stringStringHashMap.put("device", AppConst.ANDROID_DEVICE);
        stringStringHashMap.put("id", videoBean.getId());
        stringStringHashMap.put("userId", App.getUserId());
        stringStringHashMap.put("isTmpl", tag);
        HttpUtils.getInstance().executePost(AppConfig.GET_DOC_CONTENT_V10, stringStringHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                L_.e(exception);
            }

            @Override
            protected void onSuccess(String response) {

                // mvpView.loadMoudelSuccess(response);
            }
        });

    }


}
